package org.lyq.erp.process.buyfulltemp.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.lyq.erp.process.buyfull.mapper.TblWxBuyfullMapper;
import org.lyq.erp.process.buyfull.pojo.TblWxBuyfull;
import org.lyq.erp.process.buyfulltemp.mapper.TblWxBuyfullTemplateMapper;
import org.lyq.erp.process.buyfulltemp.pojo.TblWxBuyfullTemplate;
import org.lyq.erp.process.goods.mapper.TblWxGoodsMapper;
import org.lyq.erp.process.userrank.mapper.TblWxUserrankMapper;
import org.lyq.erp.process.userrank.pojo.TblWxUserrank;
import org.lyq.erp.process.userrank.pojo.TblWxUserrankExample;
import org.lyq.erp.utils.format.FormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TblWxBuyfullTemplateServiceImpl implements TblWxBuyfullTemplateService {
	@Resource
	private TblWxBuyfullTemplateMapper tblWxBuyfullTemplateMapper;
	@Resource
	private TblWxBuyfullMapper tblWxBuyfullMapper;
	@Resource
	private TblWxGoodsMapper tblWxGoodsMapper;
	@Resource
	private TblWxUserrankMapper tblWxUserrankMapper;

	@Transactional
	public void insertUserrankSelective(TblWxBuyfullTemplate buyfullTemp) {
		buyfullTemp.setBuyfullTempCreatetime(Long.valueOf(System.currentTimeMillis()));
		buyfullTemp.setBuyfullTempStarttime(
				Long.valueOf(FormatUtils.getDate(buyfullTemp.getBuyfullTempStarttimeView(), "yyyy-MM-dd").getTime()));
		buyfullTemp.setBuyfullTempEndtime(
				Long.valueOf(FormatUtils.getDate(buyfullTemp.getBuyfullTempEndtimeView(), "yyyy-MM-dd").getTime()));
		buyfullTemp.setBuyfullTempStatus(Integer.valueOf(1));
		this.tblWxBuyfullTemplateMapper.insertSelective(buyfullTemp);

		setbuyfullList(buyfullTemp, buyfullTemp.getBuyfullAmt(), buyfullTemp.getBuyfullSubtAmt());

		if (buyfullTemp.getBuyfullTempId() != null && buyfullTemp.getBuyfullTempId().intValue() != 0
				&& buyfullTemp.getBuyFullList() != null) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("buyfullTempId", buyfullTemp.getBuyfullTempId());
			map.put("list", buyfullTemp.getBuyFullList());
			this.tblWxBuyfullMapper.insertBuyfullList(map);

			map = null;
		}
	}

	@Transactional
	public void deleteBuyfull(Integer buyfullTempId) {

		TblWxUserrankExample userrankExample = new TblWxUserrankExample();

		TblWxUserrankExample.Criteria userrankCriteria = userrankExample.createCriteria();
		userrankCriteria.andRankBuyfullIdEqualTo(buyfullTempId);
		List<TblWxUserrank> userranklist = this.tblWxUserrankMapper.selectByExample(userrankExample);
		if (userranklist != null) {
			for (TblWxUserrank tblWxUserrank : userranklist) {
				tblWxUserrank.setRankIstype(Integer.valueOf(0));
				tblWxUserrank.setRankBuyfullId(null);
				tblWxUserrank.setRankDiscntStatime(null);
				tblWxUserrank.setRankDiscntEndtime(null);
				this.tblWxUserrankMapper.updateByPrimaryKey(tblWxUserrank);
			}
		}

		this.tblWxBuyfullTemplateMapper.deleteByPrimaryKey(buyfullTempId);
		this.tblWxBuyfullMapper.deleteByPrimaryKey(buyfullTempId);
	}

	public List<TblWxBuyfullTemplate> selectAllBuyfullTemp() {
		return this.tblWxBuyfullTemplateMapper.selectAllBuyfullTemp();
	}

	@Transactional
	public void updateUserrankByPrimaryKeySelective(TblWxBuyfullTemplate buyfullTemp) {
		buyfullTemp.setBuyfullTempStarttime(
				Long.valueOf(FormatUtils.getDate(buyfullTemp.getBuyfullTempStarttimeView(), "yyyy-MM-dd").getTime()));
		buyfullTemp.setBuyfullTempEndtime(
				Long.valueOf(FormatUtils.getDate(buyfullTemp.getBuyfullTempEndtimeView(), "yyyy-MM-dd").getTime()));

		this.tblWxBuyfullTemplateMapper.updateByPrimaryKeySelective(buyfullTemp);

		setbuyfullList(buyfullTemp, buyfullTemp.getBuyfullAmt(), buyfullTemp.getBuyfullSubtAmt());

		this.tblWxBuyfullMapper.deleteByPrimaryKey(buyfullTemp.getBuyfullTempId());

		if (buyfullTemp.getBuyfullTempId() != null && buyfullTemp.getBuyfullTempId().intValue() != 0
				&& buyfullTemp.getBuyFullList() != null) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("buyfullTempId", buyfullTemp.getBuyfullTempId());
			map.put("list", buyfullTemp.getBuyFullList());
			this.tblWxBuyfullMapper.insertBuyfullList(map);

			map = null;
		}
	}

	public TblWxBuyfullTemplate selectBuyfullTempByPrimaryKey(Integer buyfullTempId) {
		return this.tblWxBuyfullTemplateMapper.selectByPrimaryKey(buyfullTempId);
	}

	private void setbuyfullList(TblWxBuyfullTemplate buyfullTemp, BigDecimal[] buyfullAmt,
			BigDecimal[] buyfullSubtAmt) {
		if (buyfullAmt != null && buyfullSubtAmt != null) {

			List<TblWxBuyfull> list = new ArrayList<TblWxBuyfull>();
			TblWxBuyfull bf = null;
			for (int i = 0; i < buyfullAmt.length; i++) {
				bf = new TblWxBuyfull();
				bf.setBuyfullAmt(buyfullAmt[i]);
				bf.setBuyfullSubtAmt(buyfullSubtAmt[i]);
				list.add(bf);
			}
			buyfullTemp.setBuyFullList(list);
		}
	}
}
